# load packages

#bupaR version 0.4.4
library(bupaR)
#lubridate version 1.7.9.2
library(lubridate)




read.csv2("ETEL.csv") %>%
  mutate(resource = "unavailable") %>% 
  mutate(Start.Timestamp = ymd_hms(Start.Timestamp),
         Complete.Timestamp = ymd_hms(Complete.Timestamp)) %>%
  activities_to_eventlog("Case.ID", activity_id = "Activity", 
                         timestamps = c("Start.Timestamp","Complete.Timestamp"), resource_id = "resource") -> MyEventLog


# Figure 2
MyEventLog %>% 
  filter_trim (end_activities = c( "LATE-GRAD" , "LATE-DROPOUT" )) %>%
  filter_precedence (antecedent = "ENR", consequent = "STOPOUT", precedence_type = "directly_follows", filter_method = "none" ) %>%
  filter_precedence (antecedent = "MUST-RETAKE-COURSES", consequent = "STOPOUT", precedence_type = "directly_follows") %>%
  filter_precedence(antecedent = "NO-RETAKE-COURSES",consequent = "STOPOUT",precedence_type = "directly_follows", filter_method = "none") %>%
  process_map(
    type_nodes = frequency("relative_case"),
    sec_nodes = frequency ("absolute_case"),
    type_edges = frequency("relative_case")
  )

# Figure 3 - a  
MyEventLog %>% 
  filter (ID_CINEUNESCO == 1 | ID_CINEUNESCO == 2 | ID_CINEUNESCO == 3 | ID_CINEUNESCO == 6) %>%
  filter_trim (end_activities = c( "LATE-GRAD" , "LATE-DROPOUT" )) %>%
  filter_precedence (antecedent = "ENR", consequent = "STOPOUT", precedence_type = "directly_follows", filter_method = "none" ) %>%
  filter_precedence (antecedent = "MUST-RETAKE-COURSES", consequent = "STOPOUT", precedence_type = "directly_follows") %>%
  filter_precedence(antecedent = "NO-RETAKE-COURSES",consequent = "STOPOUT",precedence_type = "directly_follows", filter_method = "none") %>%
  process_map(
    type_nodes = frequency("relative_case"),
    sec_nodes = frequency ("absolute_case"),
    type_edges = frequency("relative_case")
  )

# Figure 3 - b
MyEventLog %>% 
  filter (ID_CINEUNESCO == 4 | ID_CINEUNESCO == 5 | ID_CINEUNESCO == 7 ) %>%
  filter_trim (end_activities = c( "LATE-GRAD" , "LATE-DROPOUT" )) %>%
  filter_precedence (antecedent = "ENR", consequent = "STOPOUT", precedence_type = "directly_follows", filter_method = "none" ) %>%
  filter_precedence (antecedent = "MUST-RETAKE-COURSES", consequent = "STOPOUT", precedence_type = "directly_follows") %>%
  filter_precedence(antecedent = "NO-RETAKE-COURSES",consequent = "STOPOUT",precedence_type = "directly_follows", filter_method = "none") %>%
  process_map(
    type_nodes = frequency("relative_case"),
    sec_nodes = frequency ("absolute_case"),
    type_edges = frequency("relative_case")
  )

# Figure 4
MyEventLog %>% 
  filter_trim (end_activities = c( "LATE-GRAD" , "LATE-DROPOUT" )) %>%
  filter_precedence (antecedent = "ENR", consequent = "STOPOUT", precedence_type = "directly_follows", filter_method = "none") %>%
  filter_precedence (antecedent = "NO-RETAKE-COURSES", consequent = "STOPOUT", precedence_type = "directly_follows") %>%
  filter_precedence(antecedent = "MUST-RETAKE-COURSES",consequent = "STOPOUT",precedence_type = "directly_follows", filter_method = "none") %>%
  process_map(
    type_nodes = frequency("relative_case"),
    sec_nodes = frequency ("absolute_case"),
    type_edges = frequency("relative_case")
  )

# Figura 5-a
MyEventLog %>% 
  filter_precedence (antecedent = "ENR", consequent = "STOPOUT", precedence_type = "directly_follows", filter_method = "none") %>%
  filter_trim (end_activities = c("LATE-DROPOUT" )) %>%
  filter_precedence (antecedent = "MUST-RETAKE-COURSES", consequent = "LATE-DROPOUT", precedence_type = "directly_follows") %>%
  process_map(
    type_nodes = frequency("relative_case"),
    sec_nodes = frequency ("absolute_case"),
    type_edges = frequency("relative_case")
  )

# Figura 5-b
MyEventLog %>% 
  filter_precedence (antecedent = "ENR", consequent = "STOPOUT", precedence_type = "directly_follows", filter_method = "none") %>%
  filter_trim (end_activities = c("LATE-DROPOUT" )) %>%
  filter_precedence (antecedent = "NO-RETAKE-COURSES", consequent = "LATE-DROPOUT", precedence_type = "directly_follows") %>%
  process_map(
    type_nodes = frequency("relative_case"),
    sec_nodes = frequency ("absolute_case"),
    type_edges = frequency("relative_case")
  )

# Figura 5-c
MyEventLog %>% 
  filter_precedence (antecedent = "ENR", consequent = "STOPOUT", precedence_type = "directly_follows", filter_method = "none") %>%
  filter_trim (end_activities = c("LATE-GRAD" )) %>%
  process_map(
    type_nodes = frequency("relative_case"),
    sec_nodes = frequency ("absolute_case"),
    type_edges = frequency("relative_case")
  )
